﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.Linq;
using CoolCode.Entities;
using CoolCode.Web.UI.Views;

namespace CoolCode.Web.UI.Controls
{
    public partial class ProductList : Partial<PaginatedList<Inspection>>, IPager
    {
        public bool AutoLoad { get; set; }

        public bool EnableCheckBox
        {
            get { return GetProperty<bool>("EnableCheckBox"); }
            set { this.SetProperty("EnableCheckBox", value); }
        }

        public event GridViewPageEventHandler PageIndexChanging;

        public ProductList()
        {
            AutoLoad = true;
            EnableCheckBox = false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
        }

        protected void handleEvents()
        {
            GridViewHelper.HighlightStatus(gvMaster);
            GridViewHelper.HighlightCriterion(gvMaster);
            pgrMaster.PageIndexChanging += PageIndexChanging;

            if (AutoLoad)
            {
                if (!Page.IsPostBack)
                {
                    gvMaster.Load += new EventHandler(gvMaster_Load);
                }
            }
            else
            {
                pgrMaster.Visible = false;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            gvMaster.Columns[(int)InspectionColumn.Command].Visible = EnableCheckBox;
            if (EnableCheckBox)
            {
                gvMaster.HandleSelectAll("chkSelectAll", "chkSelected");
            }
        }

        protected void gvMaster_Load(object sender, EventArgs e)
        {
            if (PageIndexChanging != null)
            {
                PageIndexChanging(sender, new GridViewPageEventArgs(0));
            }
        }

        public override void DataBind()
        {
            //base.DataBind();
            BindData_Master();
        }

        private void BindData_Master()
        {
            gvMaster.DataSource = Model;
            gvMaster.DataBind();
            pgrMaster.Bind(Model);
        }

        public List<string> GetSelections()
        {
            List<string> selected = gvMaster.GetSelections<string>("chkSelected", (int)InspectionColumn.ProductID);
            return selected; 
        }

        public void SelectAll()
        {
            gvMaster.SelectAll("chkSelected"); 
        }

    }


}